<?php
require_once("ErrCode.php");
class Arcitle
{
	private $_db ;
	function __construct($db)
	{
		$this->_db = $db ;
	}
	public  function create($title,$content,$userId){
		// if(empty($username)){
		// 	throw new Exception("用户名不能为空",ErrCode::USERNAME_CONNOT_EMPTY);
		// }
		// if(empty($password)){
		// 	throw new Exception("密码不能为空",ErrCode::PASSWORD_EMPTY);
		// }
		// if($this->_isUserNameExsit($username)){
		// 	throw new Exception("用户名已存在",ErrCode::USERNAME_EXIST);
		// }
		//写入数据库
		$create_time = Date("Y-m-d H:i:s",time());
		$sql = "INSERT INTO `arcitle`(`title`,`content`,`create_time`,`user_id`) VALUE(:title,:content,:create_time,:user_id)";
		$stmt = $this->_db->prepare($sql);
		$stmt->bindParam(":title",$title);
		$stmt->bindParam(":content",$content);
		$stmt->bindParam(":create_time",$create_time);
		$stmt->bindParam(":user_id",$userId);
		if(!$stmt->execute()){
			throw new Exception("文章添加失败",ErrCode::REGISTER_FAIL);
		}
		return [
			'userId' => $this->_db->lastInsertId(),
			'title' => $title ,
			'content' => $content ,
			'create_time' => $create_time,
			'user_id' => $userId
		];	
	}


	public function view($id){
		$sql = "SELECT * FROM `arcitle` WHERE `arcitle_id`=:id ";
		$stmt = $this->_db->prepare($sql);
		$stmt->bindParam(":id",$id);
		$stmt->execute();
		$arcitle = $stmt->fetch(PDO::FETCH_ASSOC);
		if(empty($arcitle)){
			throw new Exception("查询失败,文章不存在",ErrCode::PASSWORD_ERROR);
		}else{
			return $arcitle;
		}
	}

	public function edit($article_id){

	}

	public function delete(){

	}

}
